package com.graduation.mathematics.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.graduation.mathematics.R;
import com.graduation.mathematics.base.BaseActivity;
import com.graduation.mathematics.bean.UserInfo;
import com.graduation.mathematics.config.Session;
import com.graduation.mathematics.constract.LoginContract;
import com.graduation.mathematics.presenter.LoginPresenter;

/**
 * Created by apc_allen
 * date : 2/23/21
 * description :
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, LoginContract.View {

    public EditText mEtName;
    public EditText mEtPassword;

    public CheckBox rememberAccount;

    public Button mBtnLogin;
    public Button mBtnRegister;

    public LoginPresenter loginPresenter;

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initDataAndEvent() {
        loginPresenter = new LoginPresenter(this);
        initView();
    }

    private void initView(){
        mEtName = findView(R.id.et_name_login_fragment);
        mEtPassword = findView(R.id.et_password_login_fragment);

        rememberAccount = findView(R.id.remember_account);

        mBtnLogin = findView(R.id.btn_login_login_fragment);
        mBtnRegister = findView(R.id.btn_login_register_fragment);

        mBtnLogin.setOnClickListener(this);
        mBtnRegister.setOnClickListener(this);
        rememberAccount.setOnCheckedChangeListener(this);

        if (SPUtils.getInstance().getBoolean("remember", false)) {
            rememberAccount.setChecked(true);
            mEtName.setText(SPUtils.getInstance().getString("account"));
            mEtPassword.setText(SPUtils.getInstance().getString("password"));
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_login_login_fragment) {
            String name = mEtName.getText().toString().trim();
            String password = mEtPassword.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort("请输入账号");
                return;
            }
            if (TextUtils.isEmpty(password)) {
                ToastUtils.showShort("请输入密码");
                return;
            }
            showLoadingDialog("正在登录···");
            loginPresenter.login(name, password);
        } else if(id == R.id.btn_login_register_fragment ){
            startActivityForResult(new Intent(this, RegisterActivity.class), 1001);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            SPUtils.getInstance().put("remember", true);
        } else {
            SPUtils.getInstance().put("remember", false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001) {
            String account = SPUtils.getInstance().getString("account");
            String password = SPUtils.getInstance().getString("password");
            mEtName.setText(account);
            mEtPassword.setText(password);
        }
    }

    @Override
    public void onLoginSuccess(UserInfo userInfo) {
        closeLoadingDialog();
        showToast("登录成功");
        Session.setScUserInfo(userInfo);
        if (userInfo.getRileId() == 0) {
            startActivity(new Intent(LoginActivity.this, StudentActivity.class));
        } else {
            startActivity(new Intent(LoginActivity.this, ParentActivity.class));
        }
        finish();
    }

    @Override
    public void onLoginFailed(String description) {
        closeLoadingDialog();
        showToast("用户不存在");
    }
}
